//
//  RecentConversionsViewController.m
//  DSNDialer
//
//  Created by Chris Morrell on 5/10/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "RecentConversionsTableViewController.h"
#import "ConversionDetailViewController.h"
#import "DirectoryDetailViewController.h"

@implementation RecentConversionsTableViewController

@synthesize recentArray;

-(IBAction)toggleEdit
{
	[self.tableView setEditing:!self.tableView.editing animated:YES];
	
	if (self.tableView.editing)
	{
		[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
	}
	else
	{
		[self.navigationItem.rightBarButtonItem setTitle:@"Delete"];
	}
}

-(void)cancelButtonAction:(id)sender
{
	[self dismissModalViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

	UIBarButtonItem *convertButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleDone target:self action:@selector(cancelButtonAction:)];
									  
	self.navigationItem.leftBarButtonItem = convertButton;
	[convertButton release];
	
	UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStylePlain target:self action:@selector(toggleEdit)];
	self.navigationItem.rightBarButtonItem = editButton;
	[editButton release];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [recentArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
	NSDictionary *thisEntry = [recentArray objectAtIndex:indexPath.row];
	cell.textLabel.text = [NSString stringWithFormat:@"DSN: %@",[thisEntry objectForKey:@"dsn"]];
	cell.detailTextLabel.text = [NSString stringWithFormat:@"Comm: %@",[thisEntry objectForKey:@"comm"]];
   return cell;
}


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return UITableViewCellEditingStyleDelete;
}


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
		[recentArray removeObjectAtIndex:indexPath.row];
       [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
		
		NSString *errorDesc;
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *documentsDirectory = [paths objectAtIndex:0]; 
		NSString *DataPath = [documentsDirectory stringByAppendingPathComponent:@"Recents.plist"];
		
		NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:recentArray format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];
		
		if (plistData) {
			[plistData writeToFile:DataPath atomically:YES];
		}
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	DirectoryDetailViewController *myConversionDetailViewController = [[DirectoryDetailViewController alloc] initWithNibName:@"DirectoryDetailView" bundle:nil];
	myConversionDetailViewController.thisPhoneBookDictionary = [recentArray objectAtIndex:indexPath.row];
	myConversionDetailViewController.title = @"Converted Number";
	
	[self.navigationController pushViewController:myConversionDetailViewController animated:YES];
	[myConversionDetailViewController release];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
	[recentArray release];
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
	self.recentArray = nil;
}


- (void)dealloc {
    [super dealloc];
	[recentArray release];
}


@end

